package com.rongma.waterdrop.manager.bmobmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rongma.waterdrop.db.WaterDropHelper;
import com.rongma.waterdrop.global.WaterDropApplication;
import com.rongma.waterdrop.model.Time;
import com.rongma.waterdrop.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by RongMa on 16/6/4.
 */

public class TimeManager
{
    private static final String DB_NAME = "time";
    private static final String COLUMN_DATE="recordDate";
    private static final String COLUMN_CODE_TIME = "codeTime";
    private static final String COLUMN_SLEEP_TIME = "sleepTime";
    private static final String COLUMN_TIME_INDEX = "timeIndex";
    private static final String COLUMN_UPLOAD = "upload";

    private static TimeManager manager = new TimeManager();

    private static WaterDropHelper waterDropHelper;

    private TimeManager()
    {

    }

    public static TimeManager getManager(Context context)
    {
        waterDropHelper = new WaterDropHelper(context);
        return manager;
    }

    //添加Time到数据库
    public void insertOrUpdateTime(Time time)
    {
        //判断是否存在当前日期的数据在数据库中
        if(isExistsTime(time.getRecordDate()))
        {
            //直接修改当前日期的数据
            updateTime(time);
        }
        else
        {
            //添加数据
            insertTime(time);
            //获取总个数
            int dataLength = getDataLength();
            //判断个数,超过7个则删除顶端数据
            if(dataLength > 7)
            {
                //获取所有的Time数据,删除index最小的那个
                deleteExtraDada();
            }
        }
    }

    private void insertTime(Time time) {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        try
        {
            ContentValues values = new ContentValues();
            values.put(COLUMN_CODE_TIME,time.getCodeTime());
            values.put(COLUMN_DATE,time.getRecordDate());
            values.put(COLUMN_SLEEP_TIME,time.getSleepTime());
            values.put(COLUMN_UPLOAD,0);
            sdb.insert(DB_NAME,null,values);
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    private void deleteExtraDada() {
        //获取所有的数据
        ArrayList<Time> times = getAllTime();
        if(times.size() >= 8)
        {
            for(int i = 7 ; i < times.size() ; i++)
            {
                deleteTimeData(times.get(i));
            }
        }
    }

    public void insertTimeList(ArrayList<Time> times)
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        ContentValues contentValues = new ContentValues();
        try
        {
            for(Time time : times)
            {
                contentValues.put(COLUMN_DATE,time.getRecordDate());
                contentValues.put(COLUMN_SLEEP_TIME,time.getSleepTime());
                contentValues.put(COLUMN_CODE_TIME,time.getCodeTime());
                contentValues.put(COLUMN_UPLOAD,0);
                sdb.insert(DB_NAME,null,contentValues);
                contentValues.clear();
            }
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    private void deleteTimeData(Time time) {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        try
        {
            sdb.delete(DB_NAME,COLUMN_DATE,new String[]{time.getRecordDate()});
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //根据ID序号获取所有的Time数据
    private ArrayList<Time> getAllTime() {
        SQLiteDatabase sdb = waterDropHelper.getReadableDatabase();
        Cursor cursor = sdb.query(DB_NAME,null,null,null,null,null,COLUMN_TIME_INDEX+" DESC");
        ArrayList<Time> times = new ArrayList<Time>();
        cursor.moveToFirst();
        try
        {
            do
            {
                Time time = new Time();
                time.setCodeTime(cursor.getLong(cursor.getColumnIndex(COLUMN_CODE_TIME)));
                time.setRecordDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
                time.setSleepTime(cursor.getLong(cursor.getColumnIndex(COLUMN_SLEEP_TIME)));
                times.add(time);
            }while (cursor.moveToNext());
        }
        finally
        {
            cursor.close();
            sdb.close();
        }
        return times;
    }


    private int getDataLength()
    {
        SQLiteDatabase sdb = waterDropHelper.getReadableDatabase();
        Cursor cursor = sdb.query(DB_NAME,null,null,null,null,null,null);
        cursor.moveToFirst();
        int result = cursor.getCount();
        return result;
    }

    private void updateTime(Time time) {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        try
        {
            ContentValues values = new ContentValues();
            values.put(COLUMN_CODE_TIME,time.getCodeTime());
            values.put(COLUMN_SLEEP_TIME,time.getSleepTime());
            sdb.update(DB_NAME,values,COLUMN_DATE+"=?",new String[]{time.getRecordDate()});
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //判断是否存在当前日期的数据在数据库中
    private boolean isExistsTime(String recordDate) {
        SQLiteDatabase sqLiteDatabase = waterDropHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query(DB_NAME,null,COLUMN_DATE+"=?",new String[]{recordDate},null,null,null);
        boolean isExists = cursor.moveToFirst();
        cursor.close();
        sqLiteDatabase.close();
        return isExists;
    }


}
